﻿package elements 
{
	
	import events.LoaderEvent;
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.IOErrorEvent;
	import flash.geom.Point;
	import gs.easing.Cubic;
	import gs.easing.Sine;
	import gs.TweenLite;
	import styles.ImageLoaderStyle;
	import util.Bounds;
	
	/**
	 * ImageLoaderBase с небольшими, как говорят многие флешеры, рюшечками(очень тупое слово)
	 * @author kasoi
	 */
	 
	public class ImageLoader extends ImageLoaderBase 
	{		
		
		[Event(name = "ioError", type = "flash.events.IOErrorEvent")]
		[Event(name = "loadComplete", type = "events.LoaderEvent")]
		
		/////////////////////////////////////////////////////////////////////////////
		// Public properties
		/////////////////////////////////////////////////////////////////////////////
		
		public function get bounds() : Bounds 
		{
			return this._bounds;
		}
		
		public function get fillProgress() : Number 
		{
			return this._fillProgress;
		}
		
		public function set fillProgress(value: Number) : void 
		{
			this._fillProgress = value;
			
			
			var color: Number = this.style.loaderColor;
			var alpha: Number = this.style.loaderAlpha;
			var style: String = this.style.loaderStyle;
			
			var w: Number = this._bounds.width;
			var h: Number = this._bounds.height;
			var r: Boolean = this.style.isRevert;
			var pos: Point = this.style.position;
			
			var x: Number = w * (pos.x / 100);
			var y: Number = h * (pos.y / 100);
			
			var p: Number = value;
			if (r) p = 1 - p;
			
			if (style == ImageLoaderStyle.HORIZONTAL)
			{
				this.background.graphics.clear();
				this.background.graphics.beginFill(color, alpha);
				this.background.graphics.drawRect(0, 0, w * p, h);
				this.background.graphics.endFill();
			}
			
			if (style == ImageLoaderStyle.VERTICAL)
			{
				this.background.graphics.clear();
				this.background.graphics.beginFill(color, alpha);
				this.background.graphics.drawRect(0, 0, w, h * p);
				this.background.graphics.endFill();
			}
			
			if (style == ImageLoaderStyle.HORISONTAL_LINE)
			{
				this.background.graphics.clear();
				this.background.graphics.beginFill(color, alpha);
				this.background.graphics.drawRect(0, y, w * p, 3);
				this.background.graphics.endFill();
			}
			
			if (style == ImageLoaderStyle.HORISONTAL_BOTTOM_LINE)
			{
				y = h - 3;
				this.background.graphics.clear();
				this.background.graphics.beginFill(color, alpha);
				this.background.graphics.drawRect(0, y, w * p, 3);
				this.background.graphics.endFill();
			}
		}
		
		/////////////////////////////////////////////////////////////////////////////
		// Privates
		/////////////////////////////////////////////////////////////////////////////
		
		private var background: Sprite;
		
		private var _bounds: Bounds;
		
		private var style: ImageLoaderStyle;
		
		private var _fillProgress: Number = 0;
		
		private var tween: TweenLite;
		
		private var smoothing: Boolean;
		
		/////////////////////////////////////////////////////////////////////////////
		// Public methods
		/////////////////////////////////////////////////////////////////////////////
		
		public function ImageLoader(url: String, bounds: Bounds = null, style: ImageLoaderStyle = null,
			resizeToBounds: Boolean = false, startLoad: Boolean = true, checkPolicyFile: Boolean = false,
			smoothing: Boolean = false)
		{
			this.smoothing = smoothing;
			this.background = new Sprite();
			this.addChild(this.background);
			if (!style) style = new ImageLoaderStyle();
			this.style = style;
			this._bounds = bounds;
			
			super(url, bounds, resizeToBounds, startLoad, checkPolicyFile);
			
			this.addEventListener(LoaderEvent.LOAD_COMPLETE, this.imageLoader_onLoad);
		}
		
		/////////////////////////////////////////////////////////////////////////////
		// Private methods
		/////////////////////////////////////////////////////////////////////////////
		
		override protected function refresh() : void
		{
			if (!this._bounds) return;
			if (this.isLoaded) return;
			if (!this.background) return;
			
			var p: Number = this.progress;
			this.tween = TweenLite.to(this, 1, { fillProgress: p, ease: Sine.easeOut } );
		}
		
		/////////////////////////////////////////////////////////////////////////////
		// Listeners
		/////////////////////////////////////////////////////////////////////////////
		
		private function imageLoader_onLoad(e: LoaderEvent) : void 
		{
			this.tween.complete(true);
			if (this.background)
			{
				this.background.graphics.clear();
				this.removeChild(this.background);
				this.background = null;
			}
			
			var image: DisplayObject = this.loader.content;
			if (this.smoothing)
			{
				var btm: Bitmap = this.loader.content as Bitmap;
				if (btm != null) btm.smoothing = true;
			}
			image.alpha = 0;
			TweenLite.to(image, 1, { alpha: 1, ease: Cubic.easeOut } );
			this.removeEventListener(LoaderEvent.LOAD_COMPLETE, this.imageLoader_onLoad);
		}
	}
	
}